local hx__raoshe = fk.CreateSkill{
  name = "hx__raoshe",
  tags = { Skill.Compulsory },
}


hx__raoshe:addEffect(fk.HpChanged, {
  anim_type = "offensive",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__raoshe.name) and data.reason ~= nil
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local car = room:getCardsFromPileByRule(".|1",1,"allPiles")
    if #car > 0 then
      room:notifySkillInvoked(player, hx__raoshe.name)
      player:addToPile("$hx__raoshe", car[1], false, hx__raoshe.name)
      local tos = table.filter(room:getOtherPlayers(player), function(p)
        return p:getHandcardNum() ~= 0
      end)
      if #tos <= #player:getPile("$hx__raoshe") then
        local a = math.random(1, 2)
        if a == 1 then
          player:broadcastSkillInvoke(hx__raoshe.name, 1)
          player:chat("公既知天命，识时务，为何要兴无名之师，犯我疆界？")
        else
          player:broadcastSkillInvoke(hx__raoshe.name, 2)
          player:chat("你若倒戈卸甲，以礼来降，仍不失封侯之位，国安民乐，岂不美哉？")
        end
        room:askToDiscard(player, {
         min_num = #tos,
         max_num = #tos,
         skill_name = hx__raoshe.name,
         cancelable = false,
         include_equip = false,
        })
      else
        local pd_targets = table.filter(tos, function (p) return player:canPindian(p) end)
        room:sortByAction(pd_targets)
        local pindian = player:pindian(pd_targets, hx__raoshe.name)
        for _, tar in ipairs(pd_targets) do
          local losers = {}
          if pindian.results[tar].winner then
            if pindian.results[tar].winner == player then
              table.insert(losers, tar)
            else
              table.insert(losers, tar)
            end
          else
            table.insert(losers, player)
            table.insert(losers, tar)
          end
          for _, p in ipairs(losers) do
            if p.dead or player.dead then return end
            local choices = {"#hx__raoshe-discard"}
            if #player:getPile("$hx__raoshe") > 0 then
              table.insert(choices, "#hx__raoshe-remove")
            end
            local a = math.random(1, 2)
            if a == 1 then
              player:broadcastSkillInvoke(hx__raoshe.name, 1)
              player:chat("公既知天命，识时务，为何要兴无名之师，犯我疆界？")
            else
              player:broadcastSkillInvoke(hx__raoshe.name, 2)
              player:chat("你若倒戈卸甲，以礼来降，仍不失封侯之位，国安民乐，岂不美哉？")
            end
            local choice = room:askToChoice(p, {
              choices = choices,
              cancelable = false,
              skill_name = hx__raoshe.name,
              prompt = "#hx__raoshe-choice::"..player.id,
            })
            if choice == "#hx__raoshe-discard" then
              local x = math.floor(math.max(0, p:getHandcardNum() - player:getHandcardNum()))
              room:askToDiscard(p, {
                min_num = x,
                max_num = x,
                skill_name = hx__raoshe.name,
                cancelable = false,
                include_equip = false,
              })
            else
              local card = room:askToChooseCardsAndChoice(player,{
                cards = player:getPile("$hx__raoshe"),
                min_num = 1,
                max_num = 1,
                choices = {"OK"},
                skill_name = hx__raoshe.name,
              })
              room:moveCardTo(card[1], Card.DiscardPile, nil,fk.ReasonPutIntoDiscardPile, hx__raoshe.name, nil, true, player)
            end
          end
        end
      end
    end
  end,
})

hx__raoshe:addEffect(fk.StartPindian, {
  anim_type = "special",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__raoshe.name) and (player == data.from or table.contains(data.tos, player))
    and #player:getPile("$hx__raoshe") > 0
  end,
  on_cost = function(self, event, target, player, data)
    if player:getHandcardNum() ~= 0 then
      return player.room:askToSkillInvoke(player, {
        skill_name = hx__raoshe.name,
        prompt = "#hx__raoshe-invoke",
      })
    else
     return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, hx__raoshe.name)
    local a = math.random(1, 2)
    if a == 1 then
      player:broadcastSkillInvoke(hx__raoshe.name, 1)
      player:chat("公既知天命，识时务，为何要兴无名之师，犯我疆界？")
    else
      player:broadcastSkillInvoke(hx__raoshe.name, 2)
      player:chat("你若倒戈卸甲，以礼来降，仍不失封侯之位，国安民乐，岂不美哉？")
    end
    if player == data.from then
      local card = room:askToChooseCardsAndChoice(player,{
        cards = player:getPile("$hx__raoshe"),
        min_num = 1,
        max_num = 1,
        choices = {"OK"},
        skill_name = hx__raoshe.name,
      })
      data.fromCard = Fk:getCardById(card[1])
    else
      local card = room:askToChooseCardsAndChoice(player,{
        cards = player:getPile("$hx__raoshe"),
        min_num = 1,
        max_num = 1,
        choices = {"OK"},
        skill_name = hx__raoshe.name,
      })
      data.results[player] = data.results[player] or {}
      data.results[player].toCard = Fk:getCardById(card[1])
    end
  end,
})


return hx__raoshe